<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	.form-inline select {
		width: 120px;
		margin-right: 20px;
	}
	#data-search-title {
		height: 30px;
	}
	#data-search-btn {
		margin-bottom: 4px;
	}
</style>
<div class="page-header">
	<h1>
		资源管理
		<span class="dropdown dropdown-source-upload2 dropdown-custom">
			<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
				<i class="ace-icon fa fa-plus"></i> 发布资源&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
			</div>
			<ul class="dropdown-menu">
				<li>
					<a href="<?= admin_url('source/upload_source?format=' . SOURCE_FORMAT_VIDEO) ?>">发布视频资源</a>
				</li>
				<li>
					<a href="<?= admin_url('source/upload_source?format=' . SOURCE_FORMAT_AUDIO) ?>">发布音频资源</a>
				</li>
				<li>
					<a href="<?= admin_url('source/upload_source?format=' . SOURCE_FORMAT_IMAGE_TEXT) ?>">发布图文资源</a>
				</li>
				<li>
					<a href="<?= admin_url('source/upload_source?format=' . SOURCE_FORMAT_OTHER) ?>">发布其他资源</a>
				</li>
			</ul>
		</span>
	</h1>
</div>
<div class="col-xs-12 content-filter">
	<div class="widget-box">
		<div class="widget-header" style="vertical-align:middle;">
			<h4 class="widget-title">
				内容筛选
			</h4>
			<div class="widget-toolbar">
				<a href="#" data-action="collapse">
					<i class="ace-icon fa fa-chevron-up"></i>
				</a>
				<a href="#" data-action="close">
					<i class="ace-icon fa fa-times"></i>
				</a>
			</div>
		</div>
		<div class="widget-body">
			<div class="widget-main">
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">标题</label>
					<div class="keyword-search-box">
						<div class="div-search">
							<input style="float:left;height:30px;width:220px;border:1px solid #669FC7;padding:0 30px 0 14px;border-radius:10px 0 0 10px !important;-moz-border-radius:10px 0 0 10px;" type="text" name="title" placeholder="搜索标题..." autocomplete="off">
							<span id="empty-search" style="position:relative;right:64px;cursor:pointer;display:none;">×</span>
							<button id="btn-search" class="btn-search" style="outline:none;float:left;height:30px;width:44px;border:1px solid #669FC7;background-color:#669FC7;border:0;border-radius:0 10px 10px 0;-moz-border-radius:0 10px 10px 0;">
								<i class="ace-icon fa fa-search nav-search-icon white"></i>
							</button>
						</div>
					</div>
				</div>
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">格式</label>
					<div class="keyword-search-box" id="div-format">
						<a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
						<?php foreach($formats as $format): ?>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=$format['value']?>"><?=$format['name']?></a>
						<?php endforeach; ?>
					</div>
				</div>
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">状态</label>
					<div class="keyword-search-box" id="div-state">
						<a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
						<?php foreach($states as $state): ?>
							<a href="javascript:;" class="keyword-search-keyword" data-id="<?=$state['value']?>"><?php preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $state['name'], $res); echo $res[0][0]; ?></a>
						<?php endforeach; ?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>编号</th>
				<th style="width:150px;word-wrap;word-break">资源标题</th>
				<th>资源格式</th>
				<th>所属年级</th>
				<th>所属科目</th>
				<th>资源类型</th>
				<th>所属出版社</th>
				<th>资源共享</th>
				<?php if($rmxUser['user_type'] == USER_TYPE_SCHOOL): ?>
					<th>上传者</th>
				<?php endif ?>
				<th>审核状态</th>
				<th>操作</th>
				<?php if($rmxUser['user_type'] == USER_TYPE_SCHOOL): ?>
					<th>审核</th>
				<?php endif ?>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'footer.php'?>
<script type="text/javascript">
	$(function(){
		// 资源列表
		var table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('source/get_source_list')?>',
			"columns": [
				{"data": function(ret){
	            	return ret.id;
				}},
				{"data": function(ret){
	            	return ret.title;
                }},
                {"data": function(ret){
	            	return ret.format;
				}},
                {"data": function(ret){
	            	return ret.grade_title;
                }},
                {"data": function(ret){
	            	return ret.subject_title;
				}},
                {"data": function(ret){
	            	return ret.type_title;
                }},		
                {"data": function(ret){
	            	return ret.publisher_title;
				}},
				{"data": function(ret){
	            	return ret.is_shared;
                }},
				<?php if($rmxUser['user_type'] == USER_TYPE_SCHOOL): ?>
					{"data": function(ret){
						return ret.username;
					}},
				<?php endif ?>
                {"data": function(ret){
	            	return ret.state;
				}},
				{"data": function(ret){
					var action = '';
						action += '<a href="<?= admin_url('source/upload_source?id=')?>' + ret.id + '" class="mr10"><i class="fa fa-edit"></i> 修改</a> &nbsp;&nbsp;';
	            		action += '<a data-id="' + ret.id + '" data-title="' + ret.title + '" class="delete_source text-danger" href="javascript:;"><i class="fa fa-times"></i> 删除</a>';
	                return action;
				}},
				<?php if($rmxUser['user_type'] == USER_TYPE_SCHOOL): ?>
					{"data": function(ret){
						var action = '';
						if(ret.state.indexOf('未审核') !== -1){
							action += '<a data-id="'+ ret.id + '" data-title="' + ret.title + '" data-action="accept" class="verify-source text-success" href="javascript:;"><i class="fa fa-check"></i> 通过 </a> &nbsp;&nbsp;&nbsp;&nbsp;';
							action += '<a data-id="'+ ret.id + '" data-title="' + ret.title + '" data-action="refuse" class="verify-source text-danger" href="javascript:;"><i class="fa fa-times"></i> 拒绝 </a>';
						}
						return action;
					}}
				<?php endif ?>
			]
		});
		// 点击关键字
		$("body").on("click", ".keyword-search-keyword", function(){
			var id = $(this).data("id");
			// 高亮当前点击对象
			$(this).parent().find(".keyword-search-keyword").removeClass("active");
			$(this).addClass("active");
			// 筛选资源
			var format = $("#div-format .active").data("id");
			var state = $("#div-state .active").data("id");
			var title = $("[name=title]").val();
			table.setAjaxData({
				"format": format,
				"state": state,
				"title": title
			});
		});
		// 敲回车就搜索
		$("[name=title]").keypress(function (e) {
                if (e.which == 13) {
                    $("#btn-search").trigger("click");
                }
		});
		// 清空搜索框
		$("[name=title]").keyup(function() {
			if($(this).val().length > 0) {
				$("#empty-search").show();
				$("#empty-search").click(function(){
					$("[name=title]").val('');
					$(this).hide();
					// 重置搜索结果
					var format = $("#div-format .active").data("id");
					var state = $("#div-state .active").data("id");
					table.setAjaxData({
						"format": format,
						"state": state
					});
				})
			} else {
				$("#empty-search").hide();
			}
		}); 
		// 标题搜索
		$("#btn-search").click(function() {
			var format = $("#div-format .active").data("id");
			var state = $("#div-state .active").data("id");
			var title = $("[name=title]").val();
			table.setAjaxData({
				"format": format,
				"state": state,
				"title": title
			});
		})
		// 删除资源
		$("body").on("click", ".delete_source", function(){
			var id = $(this).data("id");
			var title = $(this).data("title");
			layer.confirm('确定删除"' + title + '"吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('source/source_delete_action')?>";
				var postData = {"id": id};
				ajax_post(postUrl, postData, function(res){
					table.draw(false);
				})
			});
		})
		// 资源审核
		$("body").on("click", ".verify-source", function(){
			var id = $(this).data("id");
			var title = $(this).data("title");
			var action = $(this).data('action');
			if(action === 'accept'){
				confirm = '确定通过"' + title + '"审核吗？' 
			} else{
				confirm = '确定拒绝"' + title + '"审核吗？' ;
			}
			layer.confirm(confirm, function(){
				// 提交数据
				var postUrl = "<?=admin_url('source/verify_source')?>";
				var postData = {
					'id': id,
					'action': action
				};
				ajax_post(postUrl, postData, function(res){
					table.draw(false);
				})
			});
		})
	})
</script>